package com.ychen.goodscenter.controllers;


import com.ychen.framework.utils.Result;
import com.ychen.framework.utils.SnowFlakeUtils;
import com.ychen.goodscenter.entity.OrderInfo;
import com.ychen.goodscenter.fafka.MessageProducer;
import com.ychen.goodscenter.service.OrderService;
import com.ychen.goodscenter.vo.req.SubmitOrderReq;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private MessageProducer messageProducer;

    @PostMapping("/submitOrder")
    public OrderInfo submitOrder(@RequestBody SubmitOrderReq req) {
        // 生成订单ID,消息幂等处理
        req.setOrderId(SnowFlakeUtils.nextId());
        return orderService.submitOrder(req);
    }


    @PostMapping("/submitOrderAsync")
    public Result submitOrderAsync(@RequestBody SubmitOrderReq req) {
        // 生成订单ID,消息幂等处理
        req.setOrderId(SnowFlakeUtils.nextId());
        messageProducer.sendOrderMessageSync(req);
        return Result.ok();
    }

}
